import lodash from 'lodash';
import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

const likeParms = 'name'.split(',');
export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch product params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.fetch(where, 1, pageOpt).then(res => {
            log.info('fetch product result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch product is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find product params----', query);
        let where = [];
        if (lodash.keys(query).length > 0) {
            lodash.mapKeys(query, (value, key) => {
                if (likeParms.indexOf(key) >= 0) {
                    where.push([`${key}`, 'like', `%${value}%`]);
                } else {
                    where.push([`${key}`, '=', `${value}`]);
                }
            });
        }
        if (where.length < 1) {
            where = null;
        }
        return data.find(where, 1).then(res => {
            log.info('find product result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('find product is err----', err);
        return err;
    });
}
export function add(product) {
    return util.resolve().then(() => {
        log.info('add product params----', product);
        product.id = util.uuid();
        return data.add(product).then(res => {
            log.info('add product result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('add product is err----', err);
        return err;
    });
}
export function edit(product) {
    return util.resolve().then(() => {
        log.info('edit product params----', product);
        return data.edit(product).then(res => {
            log.info('edit product result----', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('edit product is err----', err);
        return err;
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete product params----', id);
        return data.del(id).then(res => {
            log.info('delete product result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete product is err----', err);
        return err;
    });
}
